/**
 * AtolCD/Spatialytics/widgets/MeasureChooser.js
 *
 * Measure Chooser
 *
 * Copyright (C) 2010 Atol CD
 * See LICENSE file under this distribution for licensing information
 * 
 * @author Vincent Plantegenet
 * @since 201003
 **/


dojo.provide("AtolCD.Spatialytics.widgets.MeasureChooser");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.require("dijit.TitlePane");
dojo.require("dijit.form.SimpleTextarea");
dojo.require("dijit.form.Button");


dojo.declare("AtolCD.Spatialytics.widgets.MeasureChooser",
		[dijit._Widget, dijit._Templated], {

	// title: String
	title: "Mesure",
	content: "",
	
	templatePath: dojo.moduleUrl("AtolCD.Spatialytics.widgets","templates/MeasureChooser.html"),
	widgetsInTemplate: true,
	
	// --- private attributes:
	_solapContext: null,
	_observerFunc: null,
	_olapAjax: null,
	_currCellSet: null,
	_measures : [],
	
	// -- postCreate function
	postCreate: function () {
		var that = this;
		var itemsJson = {
			identifier: 'id',
			label: 'label',
			items: []};
			
			
		var dataStore = new dojo.data.ItemFileWriteStore({ data: itemsJson });
		this.filteringSelectWidget.attr('searchAttr', 'label');
		this.filteringSelectWidget.attr('store', dataStore);
		this.filteringSelectWidget.attr('onChange',
				function() {
					that._handleChange();
				}
		);
		this.inherited(arguments);
	},
	

	// --- private methods
	_handleChange: function () {
		this._updateCurrentMap();
	},
	
	//Updates the map with new measure values
	_updateCurrentMap: function () {
		var item = this._getCurrentItemAttr();

		if (item != null) {
			this._currentId = this.filteringSelectWidget.attr('item').id;
			this._solapContext.getMapStyle().getFeaturizer().setSlicer(this._currentId);
			this._solapContext._refreshSOLAPLayer();
		}
	},
	
	// --- getters/setters
	
	_setTitleAttr: function (/*String*/ title) {
		this.title = title;
		this.measureWidget.attr('title', this.title);
	},

	_getCurrentItemAttr: function () {
		return this.filteringSelectWidget.attr('item');
	},
	// --- public methods
	
	getOlapAjaxObserver: function () {
		var that = this;
		
		var closure = function(olapAjax) {
			that._currCellSet = olapAjax.getCellSet();
			//Remise a zero :
			var itemsJson = {
				identifier: 'id',
				label: 'label',
				items: []};
				
			var dataStore = new dojo.data.ItemFileWriteStore({ data: itemsJson });
			that.filteringSelectWidget.attr('store', dataStore);
			
			for(var i in that._currCellSet.axes[0].members[0])  	
			{
				that.addMeasure(i,that._currCellSet.axes[0].members[0][i].name);
			}
			
		};
		
		return closure;
	},
	
	addMeasure: function (/*integer*/ id,
	/*String*/ label) {
		
		//Mise a jour des valeurs des mesures
		this.filteringSelectWidget.attr('store').newItem({id: id, label: label});
		this._measures[id] = label;
		
		if(id == 0) {
			// first MapStyle added, set the current item in the FilteringSelect
			// with the added MapStyle
			this.filteringSelectWidget.attr('value', 0);
		}

	},
	
	attachSOLAPContext: function (/*GeoSOA.Spatialytics.SOLAPContext*/ solapContext) {
		this._solapContext = solapContext;
	},
	
	detachSOLAPContext: function () {
		if (this._solapContext != null) {
			this._solapContext = null;
		}
	},
	
	attachOlapAjax: function (/*GeoSOA.olap4js.OlapAjax*/ olapAjax, /*Boolean*/ observe) {
		this._olapAjax = olapAjax;
		if (observe) {
			this._observerFunc = this.getOlapAjaxObserver();
			this._olapAjax.afterResponseObservable.register(this._observerFunc);
		}
	},
	
	detachOlapAjax: function () {
		if (this._olapAjax != null) {
			if (this._observerFunc != null) {
				this._olapAjax.afterResponseObservable.unregister(this._observerFunc);
				this._observerFunc = null;
			}
			this._olapAjax = null;
			
		}
	}
	
	// ---
	
});
